<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
use api\wxapp\model\Goods;
use api\wxapp\model\User;
use api\wxapp\model\Coupon;
use api\wxapp\model\Shop;
use api\wxapp\model\Diary;
class IndexController extends ApiBaseController
{
    //首页轮播图
    public function index()
    {
        $list['banner'] = db::name('admin_banner')->where('model','index_model')->order('sort desc')->select();
        $list['vip'] = db::name('admin_banner')->where('model','index_vip')->order('id desc')->find();
        $list['sale'] = db::name('admin_banner')->where('model','index_sale')->order('id desc')->find();
        $this->success('请求成功!', $list);
    }
    public function news()
    {
        $data['method'] = $this->getMethod();
        $data['controller'] = $this->getContro();
        $data['action'] = $this->getAction();
        $data['param'] = $this->getParam();
        $this->error('请求成功!', $data);
    }
    //首页精选商品
    public function hot()
    {
        $params = $this->getParam();
        $page = isset($params['page']) ? $params['page'] : 1;
        $list = Goods::field('id,name,img')->order('sales desc')
            ->limit(($page-1)*6,6)->select();
        $this->success('请求成功!', $list);
    }
    public function star()
    {
        $params = $this->getParam();
        $this->checkshopid($params);
        $page = isset($params['page']) ? $params['page'] : 1;
        $list = Goods::alias('t1')
            ->join('app_shopgoods t2','t2.goods_id = t1.id')
            ->field('t1.id,t1.name,t1.img,t2.price,t1.sales')
            ->where('t2.is_public',2)
            ->limit(($page-1)*2,2)
            ->select();
        $this->success('请求成功!', $list);
    }
    //绑定店铺页面推荐店铺 $lat 维度  $lng
    public function matchshop()
    {
        $params = $this->getParam();
        $page = isset($params['page']) ? $params['page'] : 1;
        !isset($params['lat']) ? $this->error('参数缺失!!!!') : '';
        !isset($params['lng']) ? $this->error('参数缺失!!!!') : '';
        $lat = $params['lat'];
        $lng = $params['lng'];
        $list = db::name('app_shop')->field('id,name,lat,lng')->select()->toArray();
        foreach($list as $k=>$v){
            $list[$k]['km'] = $this->GetDistance($lat,$lng,$v['lat'],$v['lng']);
            $list[$k]['data'] = Goods::alias('t1')
                    ->join('app_shopgoods t2','t2.goods_id = t1.id')
                    ->field('t1.img,t1.id,t2.price')
                    ->where('t2.shop_id',$v['id'])
                    ->order('t1.sales desc')
                    ->limit(3)
                    ->select();
        }
        //数组截取  排序
        foreach ($list as $key => $row) {
            $km[$key] = $row['km'];
        }
        array_multisort($km, SORT_ASC, $list);
        $this->success('店铺推荐!',array_slice($list,($page-1)*10,10));
    }
    //根据经纬度计算距离
    private function GetDistance($lat1, $lon1, $lat2,$lon2)
    {
        $radius = 6378.137;
        $rad = floatval(M_PI / 180.0);

        $lat1 = floatval($lat1) * $rad;
        $lon1 = floatval($lon1) * $rad;
        $lat2 = floatval($lat2) * $rad;
        $lon2 = floatval($lon2) * $rad;

        $theta = $lon2 - $lon1;

        $dist = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta));

        if ($dist < 0 ) {
            $dist += M_PI;
        }
        $dist = $dist * $radius;
        $dist = floor($dist *1000);
        return $dist;
    }
    //用户登录 赠送优惠券
    public function coupon()
    {
        //查询当前发布优惠券 并且用户没有领取过的 并且没有过期的
        $coupon = new Coupon;
        $list = $coupon->getList();
        $this->success('优惠券!',$list);
    }
    //领取优惠券
    public function couponAdd()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        !isset($params['coupon_id']) ? $this->error('参数缺失!!!!') : '';
        $info = db::name('app_coupon_list')
            ->where(['coupon_id'=>$params['coupon_id'],'uid'=>$params['uid']])
            ->find();
        if($info){
            $this->error('已经领取过,不能重复领取!!!');
        }
        $data['addtime'] = date('Y-m-d H:i:s',time());
        $res = db::name('app_coupon_list')->insert($data);
        if($res){
            $this->success('领取成功!');
        }else{
            $this->error('领取失败');
        }
    }
    //关于束式
    public function about()
    {
        $info = db::name('app_about')->find();
        $this->success('关于束式!',$info);
    }
    //用户日记
    public function diary()
    {
        $params = $this->getParam();
        $diary = new Diary;
        switch($this->getMethod()){
            case 'GET':
                $page = isset($params['page']) ? $params['page'] : 1;
                $list = $diary->getList($page,0);
                $this->success('用户日记!',$list);
            break;
            case 'POST':
                $this->checkid($params);
                $info = $diary->info($params['id']);
                $this->success('日记详情!',$info);
            break;
        }
    }
}
